package com.net.xpay.common.manager.impl.partner;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.net.common.annotation.ReadTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractVersionedManager;
import com.net.xpay.common.dao.partner.PartnerMapper;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.enums.user.SecurityStatus;
import com.net.xpay.common.manager.partner.PartnerManager;
import com.net.xpay.common.model.GetPartnerInfoModel;
import com.net.xpay.common.model.search.PartnerSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@ReadTransactional
public class PartnerManagerImpl extends AbstractVersionedManager<Partner> implements PartnerManager{

    @Autowired
    private PartnerMapper mapper;


    @Override
    protected BasicMapper<Long, Partner> getMapper() {
        return mapper;
    }

    @Override
    public List<Partner> search(PartnerSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(PartnerSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public Partner findByMobile(String mobile) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(mobile));
        Partner condition = new Partner();
        condition.setMobile(mobile);
        return getByCondition(condition);
    }

    @Override
    public List<Partner> listAll() {
        return listByCondition(new Partner(), Lists.newArrayList());
    }

    @Override
    public List<Partner> list(PartnerSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.list(searchModel));
    }

    @Override
    public List<Partner> infoList(GetPartnerInfoModel model) {
        Partner partner = new Partner();
        partner.setLevel(model.getLevel());
        partner.setSecurityStatus(SecurityStatus.NORMAL);
        return listByCondition(partner, Lists.newArrayList());
    }
}
